package stackdemon;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 21626
 * Date: 2024-10-27
 * Time: 0:11
 */
public class MyStack {//拿数组实现
    public int[] elem;
    public int usedsize;
    public MyStack(){
        elem = new int[10];
    }
    public void push(int val){
        //1.验满，扩容
        if(isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        //2.入栈
        elem[usedsize] = val;
        //3.
        usedsize++;
    }
    public boolean isFull() {
        return usedsize == elem.length;
    }
    public int pop(){
        if(isEmpty()){
            return -1;
        }
        int OldVal = elem[usedsize-1];
        usedsize--;
        return OldVal;
    }
    public boolean isEmpty(){
        return usedsize == 0;
    }
    public int peek(){
        if(isEmpty()){
            return -1;
        }
        return elem[usedsize-1];
    }
    public int getUsedsize(){
        return usedsize;
    }
}
